package operations;

import books.Book;
import books.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");

        System.out.println("请输入要借阅的图书名");

        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        // 遍历booklist 读取所有图书信息
        int flag = 0; // 最后判断是否借阅成功
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getPos(i);

            if (name.equals(book.getName())) {
                if (book.isBorrowed()) {
                    flag = 1;
                    System.out.println("该书已经被借出");
                } else {
                    book.setBorrowed(true);
                    System.out.println("图书借阅成功");
                    flag = 1;
                    break;
                }
            }
        }
        if(flag == 0){
            System.out.println("要借阅的图书不存在");
        }
    }
}
